<?php 

namespace Common\Util;

class RSA 
{

	protected $request = NULL;
	protected $private_key;
	protected $public_key;


	public function __construct($publicKey, $privateKey)
	{
		$this->public_key = $publicKey;
		$this->private_key = $privateKey;
	}

	public function privateDecrypt($encrypted)
	{
		$encrypted =  str_replace(' ', '+', $encrypted);
		$encrypteds = explode('|', $encrypted);
		$request = '';
		foreach ($encrypteds as $key => $value) {
			openssl_private_decrypt(base64_decode($value),$decrypted,$this->private_key); 
			if (!$decrypted){
				return $this;
			}
			$request .= $decrypted;
		}
		$this->request = $request;
		return $this;
		// $this->reqs = json_decode($json, true);
	}

	public function getRequest()
	{
		return $this->request;
	}

	public function toJson()
	{
		return $this->request ? json_decode($this->request, true) : NULL;
	}

	public function toArray()
	{
		return $this->request ? json_decode($this->request, true) : [];
	}


}